package com.codename1.samples;


import com.codename1.components.ToastBar;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Button;
import com.codename1.ui.CN;
import com.codename1.ui.TextArea;
import com.codename1.ui.TextField;
import com.codename1.ui.layouts.GridLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class ClipboardSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        TextField text = new TextField();
        Button copyBtn = new Button("Copy");
        copyBtn.addActionListener(evt->{
            Display.getInstance().copyToClipboard(text.getText());
        });
        Button pasteBtn = new Button("Paste");
        pasteBtn.addActionListener(evt->{
            if ("html5".equalsIgnoreCase(CN.getPlatformName())) {
                // In the browser, we don't have permission, in general, to read from the clipboard
                // but the user can initiate a paste using Ctrl-V or Cmd-V, or Edit > Paste, 
                // and the data will be received in the paste listener.
                Dialog.show("Help", "Please key-codes or Edit > Paste to paste content.", "OK", null);
                return;
            }
            handlePaste(text);
        });
        
        // The paste listener is informed when the user initiates a paste using 
        // key-codes or browser menu items (Edit > Paste).  This is currently only
        // used by the Javascript port.
        hi.addPasteListener(evt->{
            handlePaste(text);
        });
        
        hi.add(text)
                .add(GridLayout.encloseIn(2, copyBtn, pasteBtn));
       
        hi.show();
    }
    
    private void handlePaste(TextArea text) {
        Object pasteData = Display.getInstance().getPasteDataFromClipboard();
        if (pasteData instanceof String) {
            text.setText((String)pasteData);
        } else {
            ToastBar.showInfoMessage("Paste data is not text");
        }
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
